<?php

namespace brand\components;

use brand\logic\BrandHelper;
use app\admin\model\Attachment;
use brand\page\Table;

class Upload extends Component
{

    public function _init()
    {
        $this->component = 'upload';
    }
    public function Form()
    {

        parent::Form();

        $fileType = $this->props['fileType'] ?? 'image';
        $limit = $this->props['limit'] ?? 1;
        $accept = $fileType == 'image' ? 'image/*' : '';
        $this->form_column->props(
            [
                'action'     => $this->upload_url.'?fileType='.$fileType,    // 文件上传地址
                'manageUrl'  => $this->manage_url,    // 文件管理地址
                'fileType' => $fileType, // 文件类型 支持image|file
                'multiple'   => !($limit == 1), // 多选
                'limit'      => $limit,
                'accept'     => $accept,
            ]
        );


        return $this->form_column;
    }


    public function Table($this_column)
    {
        parent::Table($this_column);
        if ($this->issetTableValue('limit') > 1 || $this->issetTableValue('fileType') == 'file'){

            return  $this_column->scopedSlots([Table::$SurfaceTable->component(['inject' => ['domProps' => 'innerHTML']])]);
        }else{

            $el = $this->config['table']['el'] ?? 'el-image';
            $style = $this->config['table']['style'] ?? ['width' => '30px','height'=>'30px'];
            return $this_column->scopedSlots(
                [
                    Table::$SurfaceTable->component(
                        [
                            'el'     => $el,
                            'style'  => $style,
                            'inject' => ['attrs' => ['src', 'array.preview-src-list']],
                        ]
                    ),
                ]
            );

        }

    }


    /**
     * 表格字段过滤
     * @param $key
     * @param $value
     */
    public function checkTableValue($key,$value)
    {
        $value = parent::checkTableValue($key, $value);
        if ($this->issetTableValue('limit') > 1  || $this->issetTableValue('fileType') == 'file'){
            //文件 或多图
            $html = "";
            if($value) {
                $images  = explode(',',$value);

                foreach ($images as $image) {
                    //如果是文件
                    if (isset($this->config['table']['fileType']) && $this->config['table']['fileType'] == 'file'){

                        $file = Attachment::getByUrl($image);
                        if (!$file) continue;
                        $html .= " <div class='download' style='width: 30px'> <a title='{$file->name} 点击下载' target='_blank' href='$image'>$file->name</a> </div>";
                    }else{
                        $html .= " <div class='el-image' style='width: 30px'> <a target='_blank' href='$image'><img class='el-image__inner el-image__preview' src='$image'></a> </div>";
                    }
                }


            }
            $value = $html;
        }
        return $value;
    }

    /**
     * 编辑/新增查询字段过滤
     */
    public function checkFormValue()
    {
        if (isset($this->config['form']['props']['limit']) && $this->config['form']['props']['limit'] > 1){
            //多图
            if($this->value && is_string($this->value)) $this->value = explode(',',$this->value);
        }
    }

    /**
     * 更新/新增提交字段过滤
     * @param $key
     * @param $value
     */
    public function checkUpdateData($key, $value)
    {
        if (isset($this->config['form']['props']['limit']) && $this->config['form']['props']['limit'] > 1){
            //多图
            if ($value) $value = implode(',',$value);
        }
        return $value;
    }





    public function DbToCurd($field, $props = [])
    {
        $column = \brand\Brand::Builder()->form_method('upload')->form_props($props)
            ->table($props)->table_action_type('upload')
            ->search(false);



        return $column;
    }


    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        $options['limit'] = \brand\Brand::Builder('上传数量','number')
            ->form_marker('多张请输入尽可能大的数字')
            ->build();

        $options['fileType'] = \brand\Brand::Builder('上传类型','radio')
            ->form_options( BrandHelper::formatOptions(['image'=>'图片','file'=>'文件']) )
            ->build();

        $cloumn = \brand\Brand::Builder('文件上传配置','arrays')
            ->form_options([$options])
            ->form_props( [
                'append'    =>  $curd_method != 'curd_method'
            ])
            ->form_visible([['prop' => $curd_method, 'value' => 'Upload']])
            ->build();



        $props = $tableConfigValue['form']['props'] ?? [];
        if (!isset($props['limit']) || $props['limit'] <= 0){
            $props['limit'] = 1;
        }
        if (!isset($props['fileType']) || !$props['fileType']){
            $props['fileType'] = 'image';
        }
        $values[] = $props;

        return ['column' => $cloumn,'value' => $values];
    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'][0] ?? [];


        $props['limit'] = $param['limit'] ?? 1;
        $props['fileType'] = $param['fileType'] ?? 'image';

        $form['props'] = $props;
        $table['action_type'] = 'upload';

        return [$form,$table];
    }

}